import libtcodpy as libtcod

#===============================================================================
# ENTER + ALT: Toggle fullscreen
# PRINT SCREEN: Save a screenshot
# ?, F1: Help
# 
# KP1: Move south west
# KP2, DOWN: Move south
# KP3: Move south east
# KP4, LEFT: Move west
# KP6, RIGHT: Move east
# KP7: Move north west
# KP8, UP: Move north
# KP9: Move north east
#
# CTRL + C: Clear message queue
# ESCAPE, SPACE: Exit things
#
# x: Examine
# g: Pick up
# i: Inventory
# d: Drop
# e: Equip/Unequip
# u: Use
# 
# >: Walk path to down stairs/Go down stairs
# <: Walk path to up stairs/Go up stairs
# ENTER: Set path to walk/Stop walking path
#===============================================================================

def handle_keyboard(game, key):
    map = game.current_map
    player = game.current_map.player
    
    #Wait for the message queue to be empty
    if len(game.hud.message_queue) > 0:
        if key.vk == libtcod.KEY_SPACE:
            game.hud.next_message()
        return
    #Exit help screen
    if game.help.show:
        if key.vk != libtcod.KEY_NONE:
            game.help.hide()
        return
    #Exit Inventory
    if game.inventory.show:
        if key.vk == libtcod.KEY_SPACE or key.vk == libtcod.KEY_ESCAPE:
            game.inventory.hide()
        else:
            for item in game.inventory.items:
                if key.c == ord(item.action_key):
                    #Drop
                    if game.inventory.action == 'Drop':
                        player.drop_item(item)
                    #Equip
                    elif game.inventory.action == 'Equip/Unequip':
                        game.inventory.equip_item(item)
                    #Use
                    elif game.inventory.action == 'Use':
                        pass
                    game.inventory.hide()
                    break
        return
    #Toggle Fullscreen
    if key.vk == libtcod.KEY_ENTER and (key.lalt or key.ralt):
        libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
    #Save Screenshot
    elif key.vk == libtcod.KEY_PRINTSCREEN:
        libtcod.sys_save_screenshot(None)
        
    #Show Help
    elif key.c == ord('?') or key.vk == libtcod.KEY_F1:
        game.help.display()
        
    #Clear Message Queue
    elif (key.c == ord('c') or key.c == ord('C')) and (key.lctrl or key.rctrl):
        game.hud.clear_queue()
        
    #Exit/Stop Stuff
    elif key.vk == libtcod.KEY_ESCAPE or key.vk == libtcod.KEY_SPACE:
        player.stop_path()
        player.hide_cursor()
        
    #Player Movement
    elif key.vk == libtcod.KEY_KP1:
        player.move(-1, 1)
    elif key.vk == libtcod.KEY_KP2 or key.vk == libtcod.KEY_DOWN:
        player.move(0, 1)
    elif key.vk == libtcod.KEY_KP3:
        player.move(1, 1)
    elif key.vk == libtcod.KEY_KP4 or key.vk == libtcod.KEY_LEFT:
        player.move(-1, 0)
    elif key.vk == libtcod.KEY_KP6 or key.vk == libtcod.KEY_RIGHT:
        player.move(1, 0)
    elif key.vk == libtcod.KEY_KP7:
        player.move(-1, -1)
    elif key.vk == libtcod.KEY_KP8 or key.vk == libtcod.KEY_UP:
        player.move(0, -1)
    elif key.vk == libtcod.KEY_KP9:
        player.move(1, -1)
        
    #Player Actions
    elif key.c == ord('x'): #Examine
        if not player.examine_cursor:
            player.show_cursor(True)
        else:
            player.hide_cursor()
    elif key.c == ord('g'): #Pick up
        player.pick_up()
    elif key.c == ord('i'): #Inventory
        game.inventory.display()
    elif key.c == ord('d'): #Drop
        game.inventory.display('Drop')
    elif key.c == ord('e'): #Equip
        game.inventory.display('Equip/Unequip')
    elif key.c == ord('u'): #Use
        game.inventory.display('Use')
    
    #Player Pathfinding    
    elif key.c == ord('>'):
        if player.x == map.stairs_down.x and player.y == map.stairs_down.y:
            game.next_map()
        else:
            player.walk_path(map.stairs_down.x, map.stairs_down.y)
    elif key.c == ord('<'):
        if player.x == map.stairs_up.x and player.y == map.stairs_up.y:
            game.prev_map()
        else:
            player.walk_path(map.stairs_up.x, map.stairs_up.y)
    elif key.vk == libtcod.KEY_ENTER:
        if player.path_walk:
            player.stop_path()
        elif not player.path_cursor:
            player.show_cursor()
        elif player.path_cursor:
            player.walk_path(player.path_cursor_x, player.path_cursor_y)
            player.hide_cursor()  
            
    #Player Debugging
    elif key.vk == libtcod.KEY_PAGEUP:
        game.player_data['hp'] += 1
    elif key.vk == libtcod.KEY_PAGEDOWN:
        game.player_data['hp'] -= 1
        
    #Map Debugging
    elif key.vk == libtcod.KEY_INSERT:
        map.lights_on = not map.lights_on

if __name__ == "__main__":
    print "Please run main.py!"
